package com.econ.powercloud.ems.observer;

import com.econ.powercloud.common.base.facade.util.JacksonUtil;
import com.econ.powercloud.common.base.facade.util.TimeUtil;
import com.econ.powercloud.ems.common.enums.*;
import com.econ.powercloud.ems.common.pojo.EmsEventAttrDataDTO;
import com.econ.powercloud.ems.common.pojo.EmsEventOriginDataDTO;
import com.econ.powercloud.ems.common.pojo.EmsKafkaEvenDataDTO;
import com.econ.powercloud.ems.common.util.IdWorker;
import com.econ.powercloud.ems.config.EMSStaticMap;
import com.econ.powercloud.ems.constant.SysConstant;
import com.econ.powercloud.ems.dal.entity.EmsDevice;
import com.econ.powercloud.ems.dal.entity.EmsFault;
import com.econ.powercloud.ems.common.interf.EventDataObserver;
import com.econ.powercloud.ems.service.biz.FaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;

/**
 * @ Description: 事件处理为故障告警信息
 * @ Author：hgp
 * @ Create:2022-05-09-14:32
 */
@Component
public class EventDataDealProcessor implements EventDataObserver {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private FaultService faultService;

    @Override
    public void doInvoke(EmsKafkaEvenDataDTO data) {
        EmsDevice emsDevice = EMSStaticMap.moduleCodeToDeviceMap.get(data.getDeviceCode()+ SysConstant.SPOT + (data.getModuleCode()!=null?data.getModuleCode():"def"));
        if(emsDevice==null){
            //没找到设备，返回
            return;
        }
        EmsFault leastFaultRecord = faultService.selectLeastFaultData(emsDevice.getDeviceCode(), data.getEventPid(), data.getEventSourceType());
        if(leastFaultRecord== null){
            //平台事件，第一次上传
            EmsFault emsFault = assembleEmsFaultByEvent(data,emsDevice);
            faultService.emsFaultToRedisAndClickHouse(emsFault);
            return;
        }
        if (!leastFaultRecord.getFaultVal().equals(data.getEmergency())){
            //平台事件,跟上一次的紧急程度不一样
            EmsFault emsFault = assembleEmsFaultByEvent(data,emsDevice);
            faultService.emsFaultToRedisAndClickHouse(emsFault);
            return;
        }
        if(leastFaultRecord.getFaultVal().equals(data.getEmergency())&& EventDealStatusType.DEAL.getCode().equals(leastFaultRecord.getDealState())){
            //平台事件,紧急程度一样,事件已处理
            EmsFault emsFault = assembleEmsFaultByEvent(data,emsDevice);
            faultService.emsFaultToRedisAndClickHouse(emsFault);
            return;
        }
    }

    /**
     * 存储时间数据
     * @param data： 事件kafka数据
     * @param emsDevice： 事件对应ems中的数据
     */
    private EmsFault assembleEmsFaultByEvent(EmsKafkaEvenDataDTO data, EmsDevice emsDevice) {
        EmsFault result = new EmsFault();
        result.setFaultPid(data.getEventPid()).setFaultName(data.getEventName())
                .setFaultResource(data.getEventSourceType()).setFaultVal(data.getEmergency()).setDealState(EventDealStatusType.UN_DEAL.getCode())
                .setDeviceCode(emsDevice.getDeviceCode()).setEnergyEquipmentId(emsDevice.getEnergyEquipmentId()).setEnergyStationId(emsDevice.getEnergyStationId());
        if(EventEmergency.GENERAL.getCode().equals(data.getEmergency())){
            //若是普通事件，处理状态设置为null
            result.setDealState(null);
            result.setFaultType(AlarmOrFaultState.NORMAL.getCode());
        }else if(EventEmergency.MIDDLE.getCode().equals(data.getEmergency())){
            result.setFaultType(AlarmOrFaultState.ALARM.getCode());
        }else{
            result.setFaultType(AlarmOrFaultState.FAULT.getCode());
        }
        if(EventSourceType.PLATFORM_ANALYSIS.getCode().equals(data.getEventSourceType())){
            EmsEventAttrDataDTO attrData = data.getAttrData();
            if(attrData!=null){
                result.setRealPidVal(attrData.getVal()).setGmtCreate(attrData.getTime())
                        .setRealPid(attrData.getPid()).setRealMeasureType(MeasureType.fromCodeInIot(attrData.getType()));
            }
        }else if(EventSourceType.EVENT_CHANNEL.getCode().equals(data.getEventSourceType())){
            EmsEventOriginDataDTO.EventParam params = data.getOriginData().getParams();
            if(params!=null){
                result.setGmtCreate(params.getTime()*1000).setRealPidVal(JacksonUtil.toJSONString(params.getValue()));
            }
        }
        result.setId(TimeUtil.format2String(result.getGmtCreate(), DateTimeFormatter.ofPattern("yyyy"))+idWorker.nextId());
        return result;
    }
}
